package com.yto.hrreport.event {
	import com.yto.hrreport.command.ReportCommand;
	import com.yto.hrreport.command.sysTemplatePanel;
	import com.yto.hrreport.event.buttons.AddButtonEvent;
	import com.yto.hrreport.event.buttons.CancleButtonEvent;
	import com.yto.hrreport.event.buttons.CardListButtonEvent;
	import com.yto.hrreport.event.buttons.DeleteButtonEvent;
	import com.yto.hrreport.event.buttons.EditButtonEvent;
	import com.yto.hrreport.event.buttons.ExportButtonEvent;
	import com.yto.hrreport.event.buttons.QueryButtonEvent;
	import com.yto.hrreport.event.buttons.RefreshButtonEvent;
	import com.yto.hrreport.event.buttons.SaveButtonEvent;
	import com.yto.hrreport.event.buttons.StatisticButtonEvent;
	import com.yto.hrreport.event.buttons.SysAddButtonEvent;
	import com.yto.hrreport.event.buttons.SysDeleteButtonEvent;
	import com.yto.hrreport.event.buttons.SysSaveButtonEvent;
	import com.yto.hrreport.event.buttons.TempAddButtonEvent;
	import com.yto.hrreport.event.buttons.TempCancleButtonEvent;
	import com.yto.hrreport.event.buttons.TempDeleteButtonEvent;
	import com.yto.hrreport.event.buttons.TempEditButtonEvent;
	import com.yto.hrreport.event.buttons.TempSaveButtonEvent;
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.Tree;
	
	public class ButtonEventHandler extends ButtonEvent {
		
		private var ui : hrreport;
		
		private static var buttonEvent : ButtonEventHandler = new ButtonEventHandler();
		private var state : String;
		
		public function ButtonEventHandler() {
			super();
		}
		
		public static function getInstance() : ButtonEventHandler {
			return buttonEvent;
		}
		
		public function onBoDelete(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			var param : Object = ReportCommand.getInstance(parentName).initParam("onBoDelete");
			if(param.headGrid.dataProvider != null && ArrayCollection(param.headGrid.dataProvider).length > 0)
				Alert.show("取消当前操作？" , "提示" , Alert.OK|Alert.CANCEL , hrreport.ui , DeleteButtonEvent.getInstance(parentName).deleteHandler );
		}
		
		public function onBoCard(event : Event) : void {
			var parentName : String = event.target.name.split("_")[0];
			CardListButtonEvent.getInstance(parentName).cardListHandler(event);
		}
		
		public function onBoQuery(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			QueryButtonEvent.getInstance(parentName).queryHandler(event);
		}
		
		public function onBoAdd(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			AddButtonEvent.getInstance(parentName).addHandler(event);
		}
		
		public function onBoSave(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			SaveButtonEvent.getInstance(parentName).saveHandler(event);
		}
		
		public function onBoCancle(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			CancleButtonEvent.getInstance(parentName).cancleHandler(event);
		}
		
		public function onBoExport(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			ExportButtonEvent.getInstance(parentName).exportToExcel(event);
		}
		
		public function onBoStatistic(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			StatisticButtonEvent.getInstance(parentName).statisticHandler(event);
		}
		
		public function onBoSave_Sys(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			SysSaveButtonEvent.getInstance(parentName).saveEvent();
		}
		
		public function onBoAdd_Sys(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			SysAddButtonEvent.getInstance(parentName).addEvent();
		}
		
		public function onBoEdit_Sys(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			EditButtonEvent.getInstance(parentName).EditEvent(true);
		}
		
		public function onBoDelete_Sys(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			SysDeleteButtonEvent.getInstance(parentName).deleteEvent();
		}
		
		public function onBoCancle_Sys(event : MouseEvent) : void {
			
			var parentName : String = Button(event.target).name.split("_")[0];
			EditButtonEvent.getInstance(parentName).EditEvent(false);
		}
		
		public function onBoRefresh_Sys(event : MouseEvent) : void {
			
			var parentName : String = Button(event.target).name.split("_")[0];
			RefreshButtonEvent.getInstance(parentName).refreshEvent();
			
		}
		
		public function onBoAdd_Template(event : MouseEvent) : void {
			
			var parentName : String = Button(event.target).name.split("_")[0];
			TempAddButtonEvent.getInstance(parentName).addEvent();
			state = "tempAdd";
		}
		
		public function onBoSave_Template(event : MouseEvent) : void { 
			var parentName : String = Button(event.target).name.split("_")[0];
			TempSaveButtonEvent.getInstance(parentName).save(state);
			
		}
		
		public function onBoEdit_Template(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			TempEditButtonEvent.getInstance(parentName).edit();
			state = "tempEdit"; 
				
		}
		
		public function onBoDelete_Template(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			TempDeleteButtonEvent.getInstance(parentName).deleteEvent();
		}
		
		public function onBoCancle_Template(event : MouseEvent) : void {
			var parentName : String = Button(event.target).name.split("_")[0];
			TempCancleButtonEvent.getInstance(parentName).cancleEvent(state);
		}
		
		public function onBoRefresh_Template(event : MouseEvent) : void {
			onBoRefresh_Sys(event);
			sysTemplatePanel.getInstance().reset()
			
		}
		
		public function initButtons(headCanvas : Canvas , tree : Tree) : void {
			
			var nodeType : Array;
			
			try {
				nodeType = headCanvas.name.split(".");
			} catch(e : Error) {
				nodeType = null;
			}
			
			if(nodeType[0] == "system")
				initPrivateSystemButtons(headCanvas , tree , nodeType.length > 1 ? nodeType[1] : "funcregister_headCanvas");
			else 
				initPrivateButtons(headCanvas , tree);
			
		}
		
		private function initPrivateSystemButtons(headCanvas : Canvas , tree : Tree , nodeType : String) : void {
			
			var btnAdd : Button = new Button();
			btnAdd.left = 5 + btnWidth * 0;
			btnAdd.label = "新增";
			btnAdd.name = tree.selectedItem.nodeid + "_btnAdd";
			btnAdd.enabled = false;
			btnAdd.addEventListener(MouseEvent.CLICK , nodeType == "funcregister_headCanvas" ? onBoAdd_Sys : onBoAdd_Template);
			
			var btnSave : Button = new Button();
			btnSave.name = tree.selectedItem.nodeid + "_btnSave";
			btnSave.left = 5 + btnWidth * 1;
			btnSave.label = "保存";
			btnSave.enabled = false;
			btnSave.addEventListener(MouseEvent.CLICK , nodeType == "funcregister_headCanvas" ? onBoSave_Sys : onBoSave_Template);
			
			var btnEdit : Button = new Button();
			btnEdit.name = tree.selectedItem.nodeid + "_btnEdit";
			btnEdit.left = 5 + btnWidth * 2;
			btnEdit.label = "修改";
			btnEdit.enabled = false;
			btnEdit.addEventListener(MouseEvent.CLICK , nodeType == "funcregister_headCanvas" ? onBoEdit_Sys : onBoEdit_Template);
			
			var btnDelete : Button = new Button();
			btnDelete.name = tree.selectedItem.nodeid + "_btnDelete";
			btnDelete.left = 5 + btnWidth * 3;
			btnDelete.label = "删除";
			btnDelete.enabled = false;
			btnDelete.addEventListener(MouseEvent.CLICK , nodeType == "funcregister_headCanvas" ? onBoDelete_Sys : onBoDelete_Template);
			
			var btnCancle : Button = new Button();
			btnCancle.name = tree.selectedItem.nodeid + "_btnCancle";
			btnCancle.left = 5 + btnWidth * 4;
			btnCancle.label = "取消";
			btnCancle.enabled = false;
			btnCancle.addEventListener(MouseEvent.CLICK , nodeType == "funcregister_headCanvas" ? onBoCancle_Sys : onBoCancle_Template);
			
			var btnRefresh : Button = new Button();
			btnRefresh.name = tree.selectedItem.nodeid + "_btnRefresh";
			btnRefresh.left = 5 + btnWidth * 5;
			btnRefresh.label = "刷新";
			btnRefresh.enabled = true;
			btnRefresh.addEventListener(MouseEvent.CLICK , nodeType == "funcregister_headCanvas" ? onBoRefresh_Sys : onBoRefresh_Template);
			
			headCanvas.addChild(btnAdd);
			headCanvas.addChild(btnSave);
			headCanvas.addChild(btnEdit);
			headCanvas.addChild(btnDelete);
			headCanvas.addChild(btnCancle);
			headCanvas.addChild(btnRefresh);
			
			
		}
		
		private function initPrivateButtons(headCanvas : Canvas , tree : Tree) : void {
			
			var btnQuery : Button = new Button();
			btnQuery.name = tree.selectedItem.nodeid + "_btnQuery";
			btnQuery.left = 5 + btnWidth * 0;
			btnQuery.label = "查询";
			btnQuery.addEventListener(MouseEvent.CLICK , onBoQuery);
			
			var btnAdd : Button = new Button();
			btnAdd.left = 5 + btnWidth * 1;
			btnAdd.label = "新增";
			btnAdd.name = tree.selectedItem.nodeid + "_btnAdd";
			btnAdd.enabled = true;
			btnAdd.addEventListener(MouseEvent.CLICK , onBoAdd);
			
			var btnSave : Button = new Button();
			btnSave.name = tree.selectedItem.nodeid + "_btnSave";
			btnSave.left = 5 + btnWidth * 2;
			btnSave.label = "保存";
			btnSave.enabled = false;
			btnSave.addEventListener(MouseEvent.CLICK , onBoSave);
			
			var btnDelete : Button = new Button();
			btnDelete.name = tree.selectedItem.nodeid + "_btnDelete";
			btnDelete.left = 5 + btnWidth * 3;
			btnDelete.label = "删除";
			btnDelete.enabled = false;
			btnDelete.addEventListener(MouseEvent.CLICK , onBoDelete);
			
			var btnExport : Button = new Button();
			btnExport.name = tree.selectedItem.nodeid + "_btnExport";
			btnExport.left = 5 + btnWidth * 4;
			btnExport.label = "导出";
			btnExport.enabled = true;
			btnExport.addEventListener(MouseEvent.CLICK , onBoExport);
			
			var btnStatistic : Button = new Button();
			btnStatistic.name = tree.selectedItem.nodeid + "_btnStatistic";
			btnStatistic.left = 5 + btnWidth * 5;
			btnStatistic.label = "统计";
			btnStatistic.enabled = false;
			btnStatistic.visible = false;
			btnStatistic.addEventListener(MouseEvent.CLICK , onBoStatistic);
			
			var btnCancle : Button = new Button();
			btnCancle.name = tree.selectedItem.nodeid + "_btnCancle";
			btnCancle.left = 5 + btnWidth * 6;
			btnCancle.label = "取消";
			btnCancle.enabled = false;
			btnCancle.visible = false;
			btnCancle.addEventListener(MouseEvent.CLICK , onBoCancle);
			
			var btnCard : Button = new Button();
			btnCard.name = tree.selectedItem.nodeid + "_btnCard";
			btnCard.left = 5 + btnWidth * 5;
			btnCard.label = "卡片界面";
			btnCard.enabled = true;
			btnCard.addEventListener(MouseEvent.CLICK , onBoCard);
			
			headCanvas.addChild(btnQuery);
			headCanvas.addChild(btnAdd);
			headCanvas.addChild(btnSave);
			headCanvas.addChild(btnDelete);
			headCanvas.addChild(btnExport);
			headCanvas.addChild(btnCard);
			headCanvas.addChild(btnCancle);
			headCanvas.addChild(btnStatistic);
			
		}
		
	}
}